package com.example.buyticketlishi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.baselibrary.Utils.StringUtil;
import com.example.buyticketlishi.R;
import com.example.buyticketlishi.bean.LiShihandSetReserveProductRequsetBean;

import java.util.List;

public class ImforIdListAdapter extends RecyclerView.Adapter<ImforIdListAdapter.ViewHolder> {

    private List<LiShihandSetReserveProductRequsetBean.ProductParamListBean.CertificateParamListBean> idList;
    private Context context;
    private LiShihandSetReserveProductRequsetBean allData;

    public ImforIdListAdapter(List<LiShihandSetReserveProductRequsetBean.ProductParamListBean.CertificateParamListBean> idList, Context context, LiShihandSetReserveProductRequsetBean allData) {
        this.idList = idList;
        this.context = context;
        this.allData = allData;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_imfor_idlist, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {

        holder.tvNumber.setText(position + 1 + "");
        holder.tvName.setText(idList.get(position).getTicketName());
        holder.tvId.setText(hideId(idList.get(position).getCertificateNo()));
        holder.idName.setText(idList.get(position).getCertificateName());

        holder.clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                holder.tvId.setText("");
                holder.idName.setText("");

                int remmoPos = position;
                //根据删除的数据，进行大数据删除
                for (int i = 0; i < allData.getProductParamList().size(); i++) {
                    List<LiShihandSetReserveProductRequsetBean.ProductParamListBean.CertificateParamListBean> certificateParamList = allData.getProductParamList().get(i).getCertificateParamList();
                    if (remmoPos >= certificateParamList.size()) {
                        remmoPos = (remmoPos - certificateParamList.size());
                    }else {
                        allData.getProductParamList().get(i).getCertificateParamList().get(remmoPos).setCertificateNo("");
                        allData.getProductParamList().get(i).getCertificateParamList().get(remmoPos).setCertificateName("");
                        break;
                    }
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return idList.size();
    }

    public static String hideId(String id){
        if (!StringUtil.isNullOrEmpty(id)){
            StringBuilder stringBuilder = new StringBuilder(id);
            stringBuilder.replace(4, 14, "********");
            return stringBuilder.toString();
        }else {
            return "";
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvName;
        private final TextView idName;
        private final TextView tvNumber;
        private final TextView tvId;
        private final Button clear;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_adapter_imfor_name);
            idName = itemView.findViewById(R.id.tv_adapter_imfor_play_name);
            tvNumber = itemView.findViewById(R.id.tv_adapter_imfor_number);
            tvId = itemView.findViewById(R.id.tv_adapter_imfor_id);
            clear = itemView.findViewById(R.id.tv_adapter_imfor_clear);
        }
    }
}
